<?php
include("../../includes/dbInfo.php");
$eventID = mysql_real_escape_string(stripslashes($_POST["eventID"]));
$swimmerID = mysql_real_escape_string(stripslashes($_POST["swimmerID"]));
$managerID = mysql_real_escape_string(stripslashes($_POST["managerID"]));
$comment = mysql_real_escape_string(stripslashes($_POST["theComment"]));

$query = "SELECT `swimmerID`, `coachID`, `eventID` 
	FROM `ls_comments` 
	WHERE `swimmerID` = '$swimmerID' 
	AND `coachID` = '$managerID' 
	AND `eventID` = '$eventID'";
// Check if this coach has written a comments on this user yet
$result = mysql_query($query) or die("Error selecting comments: " . mysql_error());
// If there are no comments to this user from this coach then insert it into database
if (mysql_num_rows($result) == 0) {
	$query = "INSERT INTO `ls_comments` (`swimmerID`, `coachID`, `comment`, `timeStamp`, `eventID`)
			VALUES('$swimmerID', '$managerID', '$comment', NOW(), '$eventID')";
	mysql_query($query) or die("Error inserting coach comment: " . mysql_error());
	echo "<center><br/>Added comment successfuly. <button onclick = 'closeBox();'>OK</button></center>";
}
// Else this must be an edit so do an uodate to the database
else {
	$query = "UPDATE `ls_comments` SET `comment` = '$comment', `timeStamp` = NOW()
			WHERE `swimmerID` = '$swimmerID'
			AND `coachID` = '$managerID'
			AND `eventID` = '$eventID'";
	mysql_query($query) or die("Error editing coach comment: " . mysql_error());
	echo "<center><br/>Edited comment successfuly. <button onclick = 'closeBox();'>OK</button></center>";
}
?>